<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

need_login();
$userAuth = OauthLimit::getOauthMap($login_user_id);

$timeTypeList = array(1 => "日", 2 => "週", 3 => "月");
$timeType = $_REQUEST['time_type'];

$tmp_date=date("YmdHi");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
$tmp_Hour = substr($tmp_date, 8, 2) * 1;
$tmp_Minute = substr($tmp_date, 10, 2) * 1;

$searchDate = $_REQUEST['start_date'];
$getDateArray = array();
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$startYear = $getDateArray[0] * 1;
$startMonth = $getDateArray[1] * 1;
$startDay = $getDateArray[2] * 1;

$startYear = $startYear > 0 ? $startYear : $tmp_year;
$startMonth = $startMonth > 0 ? $startMonth : $tmp_mon;
$startDay = $startDay > 0 ? $startDay : $tmp_day;



if( $defaultShopId < 1 ){
	$defaultShopId = 1;
}
$startTime = mktime(0, 0, 0, $startMonth, $startDay, $startYear);
$endTime = null;
if( $timeType == 1 ){
	$endTime = mktime(23, 59, 59, $startMonth, $startDay, $startYear);
}else if( $timeType == 2 ){
	$endTime = mktime(23, 59, 59, $startMonth, $startDay + 6, $startYear);
}else if( $timeType == 3 ){
	$endTime = mktime(23, 59, 59, $startMonth + 1, $startDay - 1, $startYear);
}else{
	$endTime = mktime(23, 59, 59, $startMonth, $startDay - 1, $startYear + 1);
}
$salaryMonthList = SalaryMonth::GetByYearMonth($defaultShopId, $startTime, $endTime);
$staffList = Staff::GetAllByShop($defaultShopId);

if( is_post() ){
	$salaryMonth = array();
	$salaryMonth['staff_id'] = $_POST['staff_id'];
	$salaryMonth['shop_id'] = $_POST['shop_id'];
	$salaryMonth['start_time'] = $startTime;
	$salaryMonth['end_time'] = $endTime;//
	$salaryMonth['tuukin_total_hour'] = $_POST['tuukin_total_hour'];
	$salaryMonth['tuukin_total_minute'] = $_POST['tuukin_total_minute'];
	$salaryMonth['rest_total_hour'] = $_POST['rest_total_hour'];
	$salaryMonth['rest_total_minute'] = $_POST['rest_total_minute'];
	$salaryMonth['work_total_hour'] = $_POST['work_total_hour'];
	$salaryMonth['work_total_minute'] = $_POST['work_total_minute'];
	$salaryMonth['reword_money'] = $_POST['reword_money'];
	$salaryMonth['bus_money'] = $_POST['bus_money'];
	$salaryMonth['batu_money'] = $_POST['batu_money'];
	$salaryMonth['total_money'] = $_POST['total_money'];
	$salaryMonth['back1'] = $_POST['back1'];
	$salaryMonth['salary'] = $_POST['salary'];
	
	if( $_POST['id'] < 1 ){
		SalaryMonth::Create($salaryMonth);
	}else{
		SalaryMonth::Update($salaryMonth);
	}
	Utility::Redirect( WEB_ROOT."/service/dataManage.php?shop_id=".$_POST['shop_id']."&staff_id=".$_POST['staff_id']);
}



include template('service/data_manage_edit');


//include template('service_staff_index');
//include template('subscribe');
